<!--
 * @Date: 2019-11-11 11:21:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-07 14:45:55
 * @describe: -添加新盘-
 -->
<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="楼盘名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="楼盘名称"></el-input>
      </el-form-item>
      <el-form-item label="楼盘别名" prop="anotherName">
        <el-input v-model="dataForm.anotherName" placeholder="楼盘别名"></el-input>
      </el-form-item>
      <el-form-item label="楼盘类型" prop="type">
        <ren-select
          v-model="dataForm.type"
          dict-type="dictType"
          placeholder="物业类型"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="区县" prop="districtCode">
        <el-select
          v-model="dataForm.districtCode"
          placeholder="请选择"
          style="width:100%"
          @change="CityRegionChange"
        >
          <el-option
            v-for="item in this.$store.state.CityRegion"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商圈" prop="districtCode">
        <el-select
          v-model="dataForm.business"
          placeholder="请选择"
          style="width:100%"
          :disabled="dataForm.districtCode==''"
        >
          <el-option
            v-for="item in businessList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>

      <el-form-item label="经纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="经纬度" @focus="getLngLat"></el-input>
      </el-form-item>

      <el-form-item label="最小面积" prop="areas">
        <el-input v-model="dataForm.areas" placeholder="最小面积"></el-input>
      </el-form-item>
      <el-form-item label="最大面积" prop="areae">
        <el-input v-model="dataForm.areae" placeholder="最大面积"></el-input>
      </el-form-item>

      <el-form-item label="单价" prop="price">
        <el-input v-model="dataForm.price" placeholder="单价"></el-input>
      </el-form-item>

      <el-form-item label="总价" prop="prices">
        <el-input v-model="dataForm.prices" placeholder="总价"></el-input>
      </el-form-item>

      <el-form-item label="佣金" prop="developersCommission">
        <el-input v-model="dataForm.developersCommission" placeholder="佣金"></el-input>
      </el-form-item>

      <el-form-item label="最新开盘时间" prop="sellTime">
        <el-date-picker
          v-model="dataForm.sellTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width:100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <!-- <el-input
          v-model="dataForm.state"
          placeholder="状态"
        ></el-input>-->
        <ren-select
          v-model="dataForm.state"
          dict-type="projState"
          placeholder="状态"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="交房时间" prop="deliverTime">
        <el-date-picker
          v-model="dataForm.deliverTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width:100%"
        ></el-date-picker>
      </el-form-item>

      <!-- <el-form-item
        label="维护人"
        prop="maintain"
      >
        <el-select
          v-model="dataForm.maintain"
          placeholder="请选择"
          style="width:100%"
        >
          <el-option
            v-for="item in this.$store.state.maintainList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
            @click.native="maintainChange(item)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="维护人部门"
        prop="maintain"
      >
        <el-input
          v-model="dataForm.deptName"
          placeholder="维护人部门"
          disabled
        ></el-input>
      </el-form-item>-->
      <el-form-item label="维护人部门">
        <el-cascader
          style="width:100%"
          v-model="deptSelectVal"
          :options="this.$store.state.DeptList"
          :show-all-levels="false"
          @change="deptIdChange"
          :props="deptProps"
          ref="myCascader"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="维护人">
        <el-select
          v-model="dataForm.maintain"
          placeholder="请选择"
          style="width:100%"
          :disabled="dataForm.deptId==''"
        >
          <el-option
            v-for="item in maintainList"
            :key="item.id"
            :label="item.realName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="content">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          v-model="dataForm.content"
          placeholder="简介"
        ></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>

    <baiduMap v-if="baiduMap_dialog" ref="getBaiduMap" @getLngLat="LngLatData"></baiduMap>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import baiduMap from "../BMap/baiduMap";
export default {
  data() {
    return {
      visible: true,
      dataForm: {
        id: "",
        name: "",
        anotherName: "",
        type: "",
        address: "",
        districtCode: "",
        business: "",
        latitude: "",
        areas: "",
        areae: "",
        price: "",
        prices: "",
        sellTime: "",
        state: "",
        deliverTime: "",
        maintain: "",
        deptName: "",
        content: "",
        developersCommission: ""
      },
      baiduMap_dialog: false,
      businessList: this.$store.state.BusinessPage,
      deptSelectVal: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      maintainList: []
    };
  },
  components: {
    baiduMap
  },
  computed: {
    dataRule() {
      return {
        paramCode: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        paramValue: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/project/projectinfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    //维护人修改
    maintainChange(item) {
      this.dataForm.deptId = item.deptId;
      this.dataForm.deptName = item.deptName;
    },
    //点击获取经纬度
    getLngLat() {
      this.baiduMap_dialog = true;
      this.$nextTick(() => {
        this.$refs.getBaiduMap.init();
      });
    },
    //获取经纬度
    LngLatData(obj) {
      this.dataForm.latitude = obj.lng + "," + obj.lat;
    },
    //行政区选择change
    CityRegionChange(val) {
      this.businessList = [];
      this.dataForm.business = "";
      this.$http
        .get(`/dictionary/business/businessList`, {
          params: {
            districtCode: val
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.businessList = res.data;
        })
        .catch(() => {});
    },
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/project/projectinfo",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
